<template>
<!--  个人资料 -->
<div class="main">
  <el-tabs type="border-card">
  <el-tab-pane>
    <span slot="label" class="labelcolor">个人资料</span>
    <el-row>
    <el-col :span="5">
      <div class="block">
      <el-upload
        class="avatar-uploader"
        action="https://jsonplaceholder.typicode.com/posts/"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="form.portrait" :src="form.portrait" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      </div>
    </el-col>
    <el-col :span="19" v-if="form.portrait=='https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'">
      <el-button type="text" class="notices"><span>请上传您的头像</span></el-button>
    </el-col>
    </el-row>
    <el-form ref="form" :model="form" label-width="80px" label-position="left">
    <el-form-item label="姓名">
      <el-input v-model="form.realname" placeholder="请输入您的真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="联系方式">
      <el-input v-model="form.phone" placeholder="请输入您的联系方式"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button class="remerber" @click="onSave">保存</el-button>
    </el-form-item>
    </el-form>
  </el-tab-pane>
</el-tabs>
</div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      form: {
          realname: '',
          phone: '',
          portrait:"https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
        }
    };
  },
  created(){
    var t=sessionStorage.getItem('ms_user')
    this.form=JSON.parse(t);
    if(this.form.portrait==''){
      this.form.portrait="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
    }
  },
  methods:{
    onSave(){
      var self=this
      var ID=sessionStorage.getItem('ms_account')
      var dataa={
        realname: self.form.realname,
        phone: self.form.phone,
        portrait:self.form.portrait,
        account:ID
      }
      axios.post('/api/user/change',JSON.stringify(dataa))
      .then((response) => {
          if (response.data == -1) {
            alert("no such a user")
          } else {
            sessionStorage.setItem('ms_user',JSON.stringify(self.form));
            alert("success")
          }                        
      }).then((error) => {

          console.log(error);
      })
    },
    handleAvatarSuccess(res, file) {
        this.form.portrait = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
  }
};
</script>
<style src="../style/mine.css">
.avatar-uploader {
    border: 1px solid #d9d9d9;
    border-radius: 50px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 100px;
    height: 100px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>